<?php

class M_course extends CI_Model{
	
	protected $last_insert_id;
	protected $insert_status;
	
	public function get_last_insert_id(){
		return $this->last_insert_id;
	}
	
	public function get_insert_status(){
		return $this->insert_status;
	}
	/*==========================Year Level============================== */
	public function get_all_levels(){
		$query = $this->db->get('year_level');
		return $query->result();
	}
	
	public function insert_year_level_fees($data){
		$data['created'] = NOW;
		$this->db->insert('year_level_fees', $data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function add_year_level($input){
		$input['created'] = NOW;
		if($this->db->insert('year_level', $input))
			$this->last_insert_id = $this->db->insert_id();
		else
			return false;
	}
	
	public function delete_year_level($level_id){
		$this->db->where('id', $level_id);
		if($this->db->delete('year_level'))
			return true;
		else
			return false;
	}
	
	public function get_year_level($level_id){
		$this->db->where('id', $level_id);
		$query = $this->db->get('year_level');
		return $query->result();
	}
	
	public function get_year_level_fees($level_id){
		$sql = 'select fee_cat_name,fee_cat_desc,ylf.fee_cat_id from year_level_fees ylf,year_level yl,fee_categories fc
				where ylf.fee_cat_id = fc.fee_cat_id
				AND ylf.year_level_id = '.$level_id.'
				group by ylf.id';
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	public function edit_year_level($level_id, $level_name){
		$data = array(
			'level_name' => $level_name,
			'updated' => NOW
		);
		
		$this->db->where('id', $level_id);
		if($this->db->update('year_level', $data))
			return true;
		else
			return false;
	}
	public function free_year_level_fees($id){
		return $this->db->where('year_level_id',$id)->delete('year_level_fees') ? TRUE : FALSE;
	}
	public function edit_year_level_fees($data){
		$this->db->set($data)->insert('year_level_fees');
		$this->insert_status = $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	/*===========================End Year Level============================= */
	
	
	/*==========================Semester============================== */
	public function get_all_semester($selected = false){
		if($selected) $this->db->where('selected', '0');
		$query = $this->db->get('semester');
		return $query->result();
	}
	
	public function add_semester($semester_name){
		$data = array('semester_name' => $semester_name, 'created' => NOW);
		
		$this->db->insert('semester', $data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function delete_semester($semester_id){
		$this->db->where('id', $semester_id);
		$this->db->delete('semester');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function get_semester($semester_id){
		$this->db->where('id', $semester_id);
		$query = $this->db->get('semester');
		return $query->result();
		
	}
	
	public function edit_semester($semester_id, $semester_name){
		$data = array(
			'semester_name' => $semester_name,
			'updated' => NOW
		);
		
		$this->db->where('id', $semester_id);
		$this->db->update('semester', $data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;

	}
	
	function set_current_semester($semester_id){

		$this->db->where('selected', 1)->update('semester', array('selected'=>0));
		$this->db->where('id', $semester_id)->update('semester', array('selected'=>1));
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	function get_current_semester(){
		$query = $this->db->where('selected', 1)->get('semester');
		return $this->db->affected_rows() > 0 ? $query->row() : FALSE;
	}
	/*===========================End Semester=============================*/	
	
	

	/*==========================Subject==============================*/
	public function get_all_subject(){
		$query = $this->db->get('subjects');
		return $query->result();
		
	}
	
	public function get_selected_subjects($id_no, $pending = false){
	
		if($pending) $data = array('id_no' => $id_no, 'status' => 'pending');
		else $data = array('id_no' => $id_no);
		$query = $this->db->select('subjects.id as id, id_no, subjectid, subject_name')
				->from('student_subjects')->join('subjects', 'student_subjects.subjectid = subjects.id', 'left')
				->where($data)->get();

		return $query->result();
	}
	
	public function get_all_available_subject($selected_subjects = null){
		$data = array();
		if(!empty($selected_subjects) && $selected_subjects != null){
			foreach($selected_subjects as $subject){
				$data[] = $subject->subjectid;	
			}
		}
		else {
			$data[] = 0;	
		}
		$query = $this->db->select('subject_name, id')->from('subjects')->where_not_in('id',$data)->get();
		return $query->result();
	}
	
	function get_approved_subjects($id_no){
		$query = $this->db->select('subjects.id as id, id_no, subjectid, subject_name')
				->from('student_subjects')->join('subjects', 'student_subjects.subjectid = subjects.id', 'left')
				->where(array('id_no'=> $id_no, 'status' => 'approved'))->get();
		return $this->db->affected_rows() > 0 ? $query->result() : FALSE;
		
	}
	
	public function add_subject($subject_name){
		$data = array(
			'subject_name' => $subject_name,
			'created' => NOW
		);
		
		if($this->db->insert('subjects', $data))
			return true;
		else
			return false;
	}
	
	public function delete_subject($subject_id){
		$this->db->where('id', $subject_id);
		if($this->db->delete('subjects'))
			return true;
		else
			return false;
	}
	
	public function get_subject($subject_id){
		$this->db->where('id', $subject_id);
		$query = $this->db->get('subjects');
		return $query->result();
		
	}
	
	public function edit_subject($subject_id, $subject_name){
		$data = array(
			'subject_name' => $subject_name,
			'updated' => NOW
		);
		
		$this->db->where('id', $subject_id);
		if($this->db->update('subjects', $data))
			return true;
		else
			return false;
	}
	
	public function add_student_subject($subjects, $id_no, $current_semester_id, $current_year_id){
		$data = array();
		$dd = array();
		for($i = 0; $i < count($subjects); $i++){
			$d = array(
				'id_no' => $id_no,
				'subjectid' => $subjects[$i],
				'status' => 'pending',
				'semester' => $current_semester_id,
				'year' => $current_year_id,
				'created' => NOW
			);
			array_push($data, $d);
		}
		

		$this->db->insert_batch('student_subjects', $data);
		
		
	}
	
	public function remove_student_subject($subjects, $id_no){
		$this->db->where('id_no', $id_no);
		$this->db->where_in('subjectid', $subjects);
		$this->db->delete('student_subjects');
		 
	}
	
	public function cancel_approvement($subjects, $id_no){
		$this->db->set(array('status' => 'pending'))->where('id_no', $id_no)->where_in('subjectid', $subjects)->update('student_subjects');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function approve_subjects($id_no,$subjects){
		$this->db->set(array('status' => 'approved'))->where('id_no', $id_no)->where_in('subjectid', $subjects)->update('student_subjects');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	
	/*===========================End Subject============================= */
	
	
	
	/*===========================start Course============================= */	
	public function get_all_course(){
		$query = $this->db->select(array('course_code','course_name','course_id','course_desc'))->get('courses');
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	public function get_course($course_id){
		$query = $this->db->select(array('course_id','course_code','course_name','course_desc','course_unit_value'))->where('course_id',$course_id)->get('courses');
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	public function update_course($data,$id){
		$this->db->where('course_id',$id)->set($data)->update('courses');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}

	public function add_course($course_data){
		$query = $this->db->insert('courses',$course_data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function delete_course($id){
		$query = $this->db->delete('courses',array('course_id'=> $id));	
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	
	
	/*===========================End Course============================= */	
	
			
	
}

?>